package com.anlu.hibenrate.test;

import com.anlu.hibernate.entity.Employee;
import com.anlu.hibernate.entity.Timekeeper;
import com.anlu.hibernate.utils.DataUtils;
import com.anlu.hibernate.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.junit.Assert.assertEquals;

/**
 * 瞬态转化成持久化状态
 */
public class TransientToPersistentTestSave {

    private SessionFactory sessionFactory;
    private Session session ;

    @Test
    public void testHelloworld(){
        System.out.println("hello world");
        assertEquals("hello","hello");
    }

    @Before
    public void before(){
        sessionFactory=HibernateUtils.getSessionFactory();
        session = sessionFactory.getCurrentSession();

        // All the action with DB via Hibernate
        // must be located in one transaction.
        // Start Transaction.
        session.getTransaction().begin();
    }

    @After
    public void after(){
        // Commit data.
        session.getTransaction().commit();
    }

    private static DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

    private static Timekeeper savepersist_Transient(Session session, Employee emp) {

        // See configuration of timekeeperId:
        // @GeneratedValue(generator = "uuid")
        // @GenericGenerator(name = "uuid", strategy = "uuid2")
        // Create an Object, Transitent state.
        Timekeeper tk2 = new Timekeeper();

        tk2.setEmployee(emp);
        tk2.setInOut(Timekeeper.IN);
        tk2.setDateTime(new Date());

        // Now 'tk3' are state Transient.
        System.out.println("- tk2 Persistent? " + session.contains(tk2));

        System.out.println("====== CALL save(tk).... ===========");

        // save() very similar to persist()
        // save() return ID, persist() return void.
        // Hibernate assign ID value to 'tk2', no action with DB
        // And return ID of 'tk2'.
        Serializable id = session.save(tk2);

        System.out.println("- id = " + id);

        //
        System.out
                .println("- tk2.getTimekeeperId() = " + tk2.getTimekeeperId());


        // Now, 'tk2' has Persistent state
        // It has been managed in Session.
        // ==> true
        System.out.println("- tk2 Persistent? " + session.contains(tk2));

        System.out.println("- Call flush..");

        // To push data into the DB, call flush().
        // If not call flush() data will be pushed to the DB when calling commit().
        // Will execute insert statement.
        session.flush();

        String timekeeperId = tk2.getTimekeeperId();
        System.out.println("- timekeeperId = " + timekeeperId);
        System.out.println("- inOut = " + tk2.getInOut());
        System.out.println("- dateTime = " + df.format(tk2.getDateTime()));
        System.out.println();
        return tk2;
    }


    @Test
    public void testSaveDemo(){
        Employee emp = null;
        try {

            emp = DataUtils.findEmployee(session, "E7499");
            savepersist_Transient(session, emp);

        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }

    



}

